package com.jshcbd.modules.asset.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jshcbd.common.util.ConvertUtils;
import com.jshcbd.framework.exception.BaseException;
import com.jshcbd.common.util.DateUtils;
import com.jshcbd.modules.asset.entity.AssetClassesEntity;
import com.jshcbd.modules.asset.entity.AssetEntity;
import com.jshcbd.modules.asset.entity.InStoreageEntity;
import com.jshcbd.modules.asset.entity.InStorageItemEntity;
import com.jshcbd.modules.asset.mapper.AssetMapper;
import com.jshcbd.modules.asset.mapper.ClassesMapper;
import com.jshcbd.modules.asset.mapper.InStoreageItemMapper;
import com.jshcbd.modules.asset.mapper.InStoreageMapper;
import com.jshcbd.modules.asset.service.IInStoreageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhoucd
 * @date
 * @description
 */
@Slf4j
@Service
@Transactional
public class StoreageServiceImpl extends ServiceImpl<InStoreageMapper, InStoreageEntity> implements IInStoreageService {

    @Autowired
    private InStoreageMapper inStoreageMapper;

    @Autowired
    private InStoreageItemMapper inStoreageItemMapper;

    @Autowired
    private AssetMapper assetMapper;

    @Autowired
    private ClassesMapper classesMapper;

    @Override
    public void deleteById(String id) throws Exception {
        InStoreageEntity entity = this.getById(id);
        this.checkUserIsEffective(entity);
        //删除明细
        inStoreageItemMapper.delete(new QueryWrapper<InStorageItemEntity>().lambda().eq(InStorageItemEntity::getMainId,id));
        //删除主表
        this.removeById(id);
    }

    @Override
    public void deleteItemById(String id) throws Exception {
        InStorageItemEntity assetItemEntity = inStoreageItemMapper.selectById(id);
        InStoreageEntity entity = this.getById(assetItemEntity.getMainId());
        this.checkUserIsEffective(entity);
        inStoreageItemMapper.deleteById(id);
    }

    @Override
    public void changeStateById(String id, Integer type) {
         LambdaQueryWrapper<InStorageItemEntity> queryWrapper = new QueryWrapper<InStorageItemEntity>()
                .lambda()
                .eq(InStorageItemEntity::getMainId,id);
        if(type == 2){
            Integer count = inStoreageItemMapper.selectCount(queryWrapper);
            if(count == 0)
                throw new BaseException("请维护资产明细！");
        }else if(type == 3){
            //生成资产信息
            List<InStorageItemEntity> items = inStoreageItemMapper.selectList(queryWrapper);
            InStoreageEntity master = inStoreageMapper.selectById(id);

            String date = DateUtils.getDate("yyMMddHHmm");
            AtomicInteger count = new AtomicInteger(1);


            Map<String,String> clasisCode = new HashMap<>();


            items.stream().map(item -> new AssetEntity(master,item)).forEach(assetEntity -> {

                String code = clasisCode.get(assetEntity.getClassesId());
                if(StringUtils.isNotEmpty(code)){
                    AssetClassesEntity classesEntity = classesMapper.selectById(assetEntity.getClassesId());
                    if(classesEntity!=null){
                        code = classesEntity.getCode();
                        clasisCode.put(classesEntity.getId(),code);
                    }
                }
                assetEntity.setState(1);
                assetEntity.setNumber(code+date
                        + ConvertUtils.generateAssetNumber(count.intValue(),4));
                assetMapper.insert(assetEntity);
                count.getAndIncrement();
            });

        }
        inStoreageMapper.updateStateById(id,type);
    }






    @Override
    public void checkUserIsEffective(InStoreageEntity entity) throws Exception {
        if(entity.getState() !=  1 && entity.getState() !=  4)
            throw new Exception("当前记录已提交不允许删除！");

    }

    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            System.out.println(DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
        }

//        String date = DateUtils.getDate("yyyyMMddHHmmss");
//        String str = "HCDSJ100021111"+date;
//
//        int hashcode = str.hashCode();
//
//        System.out.println( hashcode);
    }




}
